package com.sfexpress.tools.report;

import java.io.File;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import com.sfexpress.tools.entity.CheckResult;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.Version;

public class FreeMarkerReporter implements Reporter {
	
	private static final String DEFAULT_REPORT_TEMPLATE = "reportTemplate.ftl";

	private CheckResult result;
	private Writer writer;
	
	public FreeMarkerReporter(CheckResult result, Writer writer) {
		this.result = result;
		this.writer = writer;
	}
	
	@Override
	public void report() throws Exception {
		Version version = Configuration.VERSION_2_3_0;
		Configuration cfg = new Configuration(version);
		cfg.setDirectoryForTemplateLoading(
				new File(Thread.currentThread().getContextClassLoader().getResource("./").toURI()));
		cfg.setObjectWrapper(new DefaultObjectWrapper(version));
		Template template = cfg.getTemplate(DEFAULT_REPORT_TEMPLATE, "UTF-8");
		Map<String, CheckResult> root = new HashMap<String, CheckResult>();
		root.put("root", result);
		template.process(root, writer);
	}
	
}
